import request from '@/utils/request'
import type { ApiResponse } from '@/utils/request'

// 时间段类型定义
export interface TimeSlot {
  slotId?: number
  dayOfWeek: string
  startTime: string
  endTime: string
  createdAt?: string
  updatedAt?: string
}

// 地点类型定义
export interface Location {
  locationId?: number
  locationName: string
  address: string
  createdAt?: string
  updatedAt?: string
}

// 可用时间段类型定义
export interface AvailableSlot {
  asId: number
  adminId: number
  slotId: number
  locationId: number | null
  assessmentType: string
  available: boolean
  publishDate: string
  publishYear: number
  publishWeek: number
  createdAt: string
  updatedAt: string
  timeSlot: TimeSlot
  location?: Location
}

// 按周组织的可用时间段
export interface WeeklyAvailableSlots {
  [weekKey: string]: AvailableSlot[]
}

// 线下评估配置
export interface OfflineSlotConfig {
  slotId: number
  locationId: number
}

// 发布线上评估参数
export interface OnlinePublishParams {
  publishDate: string
  assessmentType: '线上'
  onlineSlotIds: number[]
}

// 发布线下评估参数
export interface OfflinePublishParams {
  publishDate: string
  assessmentType: '线下'
  offlineConfigs: OfflineSlotConfig[]
}

/**
 * 添加时间段
 * @param data 时间段信息
 * @returns Promise
 */
export function addTimeSlot(data: TimeSlot): Promise<ApiResponse<TimeSlot>> {
  return request({
    url: '/time-location/time-slot/add',
    method: 'post',
    data,
  })
}

/**
 * 查询所有时间段
 * @returns Promise
 */
export function getTimeSlotList(): Promise<ApiResponse<TimeSlot[]>> {
  return request({
    url: '/time-location/time-slot/list',
    method: 'get',
  })
}

/**
 * 添加地点
 * @param data 地点信息
 * @returns Promise
 */
export function addLocation(data: Location): Promise<ApiResponse<Location>> {
  return request({
    url: '/time-location/location/add',
    method: 'post',
    data,
  })
}

/**
 * 查询所有地点
 * @returns Promise
 */
export function getLocationList(): Promise<ApiResponse<Location[]>> {
  return request({
    url: '/time-location/location/list',
    method: 'get',
  })
}

/**
 * 发布线上可用时间段
 * @param data 发布参数
 * @returns Promise
 */
export function publishOnlineAvailableSlots(
  data: OnlinePublishParams,
): Promise<ApiResponse<boolean>> {
  return request({
    url: '/time-location/available-slot/publish',
    method: 'post',
    data,
  })
}

/**
 * 发布线下可用时间段和地点
 * @param data 发布参数
 * @returns Promise
 */
export function publishOfflineAvailableSlots(
  data: OfflinePublishParams,
): Promise<ApiResponse<boolean>> {
  return request({
    url: '/time-location/available-slot/publish',
    method: 'post',
    data,
  })
}

/**
 * 查询可用时间段和地点
 * @param assessmentType 评估类型，可选值：线上、线下
 * @param futureWeeks 查询未来几周（可选，默认4周）
 * @returns Promise
 */
export function getAvailableSlotList(
  assessmentType: string,
  futureWeeks?: number,
): Promise<ApiResponse<WeeklyAvailableSlots>> {
  return request({
    url: '/time-location/available-slot/list',
    method: 'get',
    params: {
      assessmentType,
      futureWeeks,
    },
  })
}
